package com.cgnpc.scp.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cgnpc.cuddemo.utils.IDHelper;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.common.bean.MyPageResult;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.utils.MyPageResultUtil;
import com.cgnpc.scp.common.utils.StringUtil;
import com.cgnpc.scp.manager.mybatis.entity.Debugtips;
import com.cgnpc.scp.manager.mybatis.entity.ProcessInfo;
import com.cgnpc.scp.manager.mybatis.mapper.client.DebugtipsMapper;
import com.cgnpc.scp.manager.service.DebugTipService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DebugTipServiceImpl implements DebugTipService {
    private static final Logger LOGGER = LoggerFactory.getLogger(DebugTipServiceImpl.class);

    @Autowired
    private DebugtipsMapper debugtipsMapper;
    @Autowired
    private CurrentUser currentUser;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveOrUpdate(Debugtips reqDataDict) throws Exception {
        int updateRows = 0;
        UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
        String loginUserInfoUserId = loginUserInfo.getUserId();
        String loginUserInfoUserName = loginUserInfo.getUserName();
            if(StringUtil.isNullOrSpace(reqDataDict.getId())){
                reqDataDict.setId(IDHelper.uuid());
                reqDataDict.setCreateUserName(loginUserInfoUserName);
                reqDataDict.setCreateUser(loginUserInfoUserId);
                reqDataDict.setCreateTime(new Date());
                reqDataDict.setLastUpdateUser(loginUserInfoUserId);
                reqDataDict.setLastUpdateUserName(loginUserInfoUserName);
                reqDataDict.setLastUpdateTime(new Date());
                updateRows = debugtipsMapper.insert(reqDataDict);
            }else {
                reqDataDict.setLastUpdateUser(loginUserInfoUserId);
                reqDataDict.setLastUpdateUserName(loginUserInfoUserName);
                reqDataDict.setLastUpdateTime(new Date());
                updateRows = debugtipsMapper.updateById(reqDataDict);
            }
        return updateRows;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int del(List<String> ids) {
        int updateRows = 0;
        try {
            debugtipsMapper.deleteBatchIds(ids);
        } catch (Exception e) {
            LOGGER.error("save.error;", e);
            throw e;
        }

        return updateRows;
    }

    @Override
    public Debugtips loadByID(Debugtips reqDataDict) throws Exception {
        return debugtipsMapper.selectById(reqDataDict.getId());
    }

    @Override
    public MyPageResult<Debugtips> loadByPaging(Debugtips reqDataDict) {
        // begin 分页开始
        // 第几页
        int pageNum = reqDataDict.getPageNum();
        // 每页的数量
        int pageSize = reqDataDict.getPageSize();
        // 创建Page对象，将pageNum，pageSize参数传入，必须位于数据库查询数据的语句之前，否则不生效
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Debugtips> page = new Page<>(pageNum, pageSize);

        // mybatis查询方式
        QueryWrapper<Debugtips> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Debugtips> order_index = queryWrapper.orderByDesc("create_time");


        // 设置查询条件
        if (!StringUtil.isNullOrSpace(reqDataDict.getContent())) {
            order_index.like("content",reqDataDict.getContent());
        }

        // 执行查询
        IPage<Debugtips> dbDataDictList = debugtipsMapper.selectPage(page,order_index);

        MyPageResult<Debugtips> myPageResult = MyPageResultUtil.createMyPageResultPlus(dbDataDictList);
        return myPageResult;
    }

}
